/// <summary>
// CodeFx.MySqlProviders - .NET Providers for MySql using the framework CodeFx
// Laurent Morisseau
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Laurent Morisseau
/// </summary>
/// <author><a href="mailto:laurent.morisseau@gmail.com">Laurent Morisseau</a></author>

namespace CodeFx.MySqlProviders
{
    using System;
    using System.Text;
    using System.Web;
    using System.Data;
    using System.ComponentModel;
    using MySql.Data.MySqlClient;
    using System.Collections;
    using CodeFx;
    using CodeFx.Web;
	using System.Collections.Generic;
    using System.Configuration;
    using System.Configuration.Provider;
		
    [DataObject]
	public static partial class Aspnet_applicationsDTO
	{
		
		#region DTO Method
		
		#region Select
        [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
		public static Aspnet_applicationsDAL Select(uint _ApplicationId)
        {
				const string command = "select * from  aspnet_applications where ApplicationId =?ApplicationId";


                MySqlConnection conn = ConnectionString.Current.OpenConnection;        
				
				MySqlCommand cmd = new MySqlCommand(command, conn);
				
cmd.Parameters.Add(new MySqlParameter("?ApplicationId", _ApplicationId));
				Aspnet_applicationsDAL dataobject = null;
				try
				{
					dataobject = Select(cmd);
				}
				finally
				{
					conn.Close();
				}
           	 	return dataobject;
        }
		
		#endregion
		
		#region SelectbyFk
		#endregion
		
		#region SelectAll
		[DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public static List<Aspnet_applicationsDAL> SelectAll()
        {
			MySqlConnection conn = ConnectionString.Current.OpenConnection;        
				
			MySqlCommand cmd = new MySqlCommand("select * from aspnet_applications", conn);
			List<Aspnet_applicationsDAL> al= null;
			try
			{
				al = SelectAll(cmd);
			}
			finally
			{
				conn.Close();
			}
       	 	return al;
        }
		#endregion
		
		#region Delete
		[DataObjectMethodAttribute(DataObjectMethodType.Delete, true)]
        public static void Delete(Aspnet_applicationsDAL dataObject)
        {
            if (dataObject == null)
                throw new ArgumentNullException("dataObject");
			
			const string command = "delete from aspnet_applications where ApplicationId =?ApplicationId";

				
				MySqlConnection conn = ConnectionString.Current.OpenConnection;        
				MySqlCommand cmd = new MySqlCommand(command, conn);
				
cmd.Parameters.Add(new MySqlParameter("?ApplicationId", dataObject.ApplicationId));
			
            	try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw new DALException(command, dataObject, ex.InnerException);
                }
				finally
				{
					conn.Close();
				}
        }
		#endregion
  
		#region ByFkDelete
		#endregion
		
		#region Insert
   		[DataObjectMethodAttribute(DataObjectMethodType.Insert, true)]
        public static void Insert(Aspnet_applicationsDAL dataObject)
        {
            if (dataObject == null)
                throw new ArgumentNullException("dataObject");
				
			const string command = "insert into aspnet_applications (ApplicationName,Description) values ( ?ApplicationName,?Description)";

				MySqlConnection conn = ConnectionString.Current.OpenConnection;        
				
				MySqlCommand cmd = new MySqlCommand(command, conn);
				
            	try
                {
                    dataObject.Write(cmd);
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw new DALException(command, dataObject, ex.InnerException);
                }
				finally
				{
					conn.Close();
				}
        }
		#endregion
		
		
		#region Update
		[DataObjectMethodAttribute(DataObjectMethodType.Update, true)]
        public static void Update(Aspnet_applicationsDAL dataObject)
        {
            if (dataObject == null)
                throw new ArgumentNullException("dataObject");
            const string command = "update aspnet_applications set ApplicationName = ?ApplicationName,Description = ?Description where ApplicationId = ?ApplicationId";
				MySqlConnection conn = ConnectionString.Current.OpenConnection;  
            MySqlCommand cmd = new MySqlCommand(command, conn); 
			try
            {
                dataObject.Write(cmd);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new DALException(command, dataObject, ex.InnerException);
            }
			finally
			{
				conn.Close();
			}
        }
		#endregion
		
		#region Exist		
		public static bool Exist(uint _ApplicationId)
        {
           	const string command = "select 1 from aspnet_applications where ApplicationId =?ApplicationId";

				MySqlConnection conn = ConnectionString.Current.OpenConnection;  
				MySqlCommand cmd = new MySqlCommand(command, conn);
				
cmd.Parameters.Add(new MySqlParameter("?ApplicationId", _ApplicationId));
				
            try
            {
            	return (cmd.ExecuteScalar() != null);
			}
            catch (Exception ex)
            {
                throw new DALException(command, ex.InnerException);
            }
			finally
			{
				conn.Close();
			}
        }

		public static bool Exist(Aspnet_applicationsDAL dataObject)
        {
            if (dataObject == null)
                throw new ArgumentNullException("dataObject");
				
           	const string command = "select 1 from aspnet_applications where ApplicationName =?ApplicationName and ApplicationId =?ApplicationId and Description =?Description";

				MySqlConnection conn = ConnectionString.Current.OpenConnection;  
				MySqlCommand cmd = new MySqlCommand(command, conn);
				
cmd.Parameters.Add(new MySqlParameter("?ApplicationName", dataObject.ApplicationName));
cmd.Parameters.Add(new MySqlParameter("?ApplicationId", dataObject.ApplicationId));
cmd.Parameters.Add(new MySqlParameter("?Description", dataObject.Description));
            try
            {
            	return (cmd.ExecuteScalar() != null);
			}
            catch (Exception ex)
            {
                throw new DALException(command, dataObject, ex.InnerException);
            }
			finally
			{
				conn.Close();
			}

        }
        #endregion	
		
		#endregion	
	
		#region Public methods
		public static Aspnet_applicationsDAL Select(MySqlCommand cmd)
        {
            if (cmd == null)
                throw new ArgumentNullException("cmd");
            	Aspnet_applicationsDAL dataObject = null;
				MySqlDataReader dr = null;
				try{
					dr = cmd.ExecuteReader(CommandBehavior.SingleResult);
                	if (dr.Read())
					{
						dataObject = new Aspnet_applicationsDAL();
						dataObject.Read(dr);
					}
				}
				catch (Exception ex)
				{
					throw new DALException(cmd.CommandText,ex.InnerException);
				}
				finally{
				if (dr != null)
					dr.Close();
				}
            	return dataObject;
        }
		
		public static List<Aspnet_applicationsDAL> SelectAll(MySqlCommand cmd)
        {
            if (cmd == null)
                throw new ArgumentNullException("cmd");
				List<Aspnet_applicationsDAL> al = null;
            	MySqlDataReader dr = null;
				try{
					dr = cmd.ExecuteReader();
					if (dr.HasRows)
					{
                    	al = new List<Aspnet_applicationsDAL>();
						while (dr.Read())
						{
							Aspnet_applicationsDAL dataObject = new Aspnet_applicationsDAL();
							dataObject.Read(dr);
							al.Add(dataObject);
						}
						al.TrimExcess();
					}
				}
				catch (Exception ex)
				{
					throw new DALException(cmd.CommandText,ex.InnerException);
				}
				finally{
				if (dr != null)
					dr.Close();
				}
            	return al;
        }
        
		#endregion
	
	}
}